package org.jxc.service;

import java.util.Date;
import java.util.List;

import org.hibernate.SessionFactory;
import org.jxc.dao.ProductDAO;
import org.jxc.domain.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.dao.DAOFactory;
import org.team4.exception.CRUDException;
import org.team4.service.IService;
import org.team4.util.LoginBean;
import org.team4.util.Validate;
import org.team4.vo.Page;

@Service
public class ProductService implements IService {

	private ProductDAO productDAO;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		productDAO = (ProductDAO) DAOFactory.getDAO(sessionFactory,
				ProductDAO.class);
	}

	/**
	 * 分页
	 * 
	 * @param page
	 * @param superId
	 * @return
	 * @throws CRUDException
	 */
	public Page<Product> listPagination(Page<Product> page, String superId)
			throws CRUDException {
		if (Validate.notNull(superId))
			return productDAO.pagination(page,
					"from Product where deleteFlag='0' and productTypeId = ?", null, superId);
		else
			return productDAO.pagination(page, "from Product where deleteFlag='0'", null);
	}

	/**
	 * 获取所有商品
	 * 
	 * @return
	 * @throws CRUDException
	 */
	public List<Product> findAllProduct(String orderBy) throws CRUDException {
		List<Product> list = productDAO.find("from Product where deleteFlag = '0'" + " ORDER BY "
				+ orderBy);
		return list;
	}

	/**
	 * (overload)获取所有商品,按默认规则(创建时间)排序
	 * 
	 * @return
	 * @throws CRUDException
	 */
	public List<Product> findAllProduct() throws CRUDException {
		return findAllProduct("createTime");
	}

	public List<Product> findProductByWarehouse(String warehouseId)
			throws CRUDException {
		return productDAO.find("from ");
	}

	/**
	 * 根据id查询商品
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public Product findById(String id) throws Exception {
		Product product = productDAO.get(id);
		return product;
	}

	/**
	 * 新建商品信息
	 * 
	 * @param product
	 * @throws CRUDException
	 */
	public void saveProduct(Product product) throws CRUDException {
		product.setDeleteFlag("0");
		product.setCreateTime(new Date());
		product.setCreator(LoginBean.getLoginBean().getUser());
		productDAO.save(product);
	}

	/**
	 * 修改商品信息
	 * 
	 * @param product
	 * @throws CRUDException
	 */
	public void updateProduct(Product product) throws CRUDException {
		Product oldProduct = productDAO.get(product.getId());
		oldProduct.setName(product.getName());
		oldProduct.setAddress(product.getAddress());
		oldProduct.setBrand(product.getBrand());
		oldProduct.setDiscountPrice(product.getDiscountPrice());
		oldProduct.setManufacturer(product.getManufacturer());
		oldProduct.setNormalPrice(product.getNormalPrice());
		oldProduct.setPriceUnit(product.getPriceUnit());
		oldProduct.setProductType(product.getProductType());
		oldProduct.setStandard(product.getStandard());
		oldProduct.setTax(product.getTax());
		oldProduct.setVipPrice(product.getVipPrice());
		oldProduct.setRemark(product.getRemark());
		oldProduct.setEditor(LoginBean.getLoginBean().getUser());
		oldProduct.setEditTime(new Date());
		productDAO.update(oldProduct);
	}

	/**
	 * 批量删除商品
	 * 
	 * @param idList
	 * @return
	 * @throws CRUDException
	 */
	public void deleteProduct(String id) throws CRUDException {
		Product p = productDAO.get(id);
		p.setDeleteFlag("1");
		productDAO.update(p);
	}

	public boolean uniqueValidate(String key, String value)
			throws CRUDException {
		List<Product> list = productDAO.find("from Product where " + key
				+ " = ?", value);
		if (list.size() > 0) {
			return true;
		}
		return false;
	}

}